package managedbean;

import bean.managed.VendorInvoiceEntityController;
import java.io.FileInputStream;
import java.io.IOException;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import javax.faces.bean.SessionScoped;
/**
 *
 * @author thanhle
 */
@ManagedBean(name = "invoiceDownload")
@SessionScoped
public class InvoiceDownload {

     @ManagedProperty(value="#{vendorInvoiceEntityController}")
    private VendorInvoiceEntityController vendorInvoiceEntityController;

    public VendorInvoiceEntityController getVendorInvoiceEntityController() {
        return vendorInvoiceEntityController;
    }

    public void setVendorInvoiceEntityController(VendorInvoiceEntityController vendorInvoiceEntityController) {
        this.vendorInvoiceEntityController = vendorInvoiceEntityController;
    }

   

 
   

    private StreamedContent file;



 

    public void setFile(StreamedContent file) {
        this.file = file;
    }
    public StreamedContent getFile() {
    
        try {
         String fileName =  vendorInvoiceEntityController.getFileName();
         String path = vendorInvoiceEntityController.getPath();
              if(path==null || path.isEmpty()){
         
              System.out.println("path unspecified");
       
              }
            file = new DefaultStreamedContent(new FileInputStream(
                   path),
                    "application/pdf", fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
      
      
      
       private String path;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
       private String fileName;
}
